package com.cnr.model;

import static javax.persistence.GenerationType.IDENTITY;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

@Entity
@Table(name = "credentials", catalog = "test", uniqueConstraints = @UniqueConstraint(columnNames = "username"))
public class Credentials implements java.io.Serializable {

	private static final long serialVersionUID = -4041109278709017100L;
	private Long id;
	private String username;
	private String password;
	private String email;
	private String role;
	private Set<Posts> posts = new HashSet<Posts>(0);

	public Credentials() {
	}

	public Credentials(String username, String password, String email,
			String role) {
		this.username = username;
		this.password = password;
		this.email = email;
		this.role = role;
	}

	public Credentials(String username, String password, String email,
			String role, Set<Posts> posts) {
		this.username = username;
		this.password = password;
		this.email = email;
		this.role = role;
		this.posts = posts;
	}

	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "id", unique = true, nullable = false)
	public Long getId() {
		return this.id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Column(name = "username", unique = true, nullable = false, length = 25)
	public String getUsername() {
		return this.username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	@Column(name = "password", nullable = false, length = 64)
	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Column(name = "email", nullable = false, length = 60)
	public String getEmail() {
		return this.email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Column(name = "role", nullable = false, length = 5)
	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "credentials")
	public Set<Posts> getPosts() {
		return this.posts;
	}

	public void setPosts(Set<Posts> posts) {
		this.posts = posts;
	}

	@Override
	public String toString() {
		return username + "[ " + role.toLowerCase() + " ]";
	}

}
